package com.yamaha.bikedesalon;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.assist.PauseOnScrollListener;
import com.yamaha.bikedesalon.util.API;
import com.yamaha.bikedesalon.util.SCStaticFunc;

public class BookingFragment extends AMyActivity implements OnItemClickListener {

	private final String TAG = this.getClass().getSimpleName();
	private final List<JSONObject> list = new ArrayList<JSONObject>();
	private final MyAdapter adapter = new MyAdapter();

	public BookingFragment() {
		super(MenuType.BOOKING);

	}
	@Override
	protected List<EditText> getEditTexts() {
		return null;
	}

	@Override
	public void onMyClick(View v) {

	}

	@Override
	protected int getLayoutId() {
		return R.layout.booking_page;
	}

	@Override
	protected void onMyCreated(Bundle savedInstanceState) {
		final ListView listView = (ListView) this.findViewById(R.id.booking_list);
		listView.setAdapter(this.adapter);
		listView.setOnItemClickListener(this);

		final boolean pauseOnScroll = true; // or true
		final boolean pauseOnFling = true; // or false
		final PauseOnScrollListener listener = new PauseOnScrollListener(this.imageLoader, pauseOnScroll, pauseOnFling);
		listView.setOnScrollListener(listener);

	}

	@Override
	protected void onResume() {
		super.onResume();
		getData(true);
	}

	private void getData(final boolean isLoading) {

		adapter.setLoading(isLoading);
		adapter.notifyDataSetChanged();

		final AsyncHttpClient client = new AsyncHttpClient();
		final RequestParams params = new RequestParams();
		params.put("user_id", SCStaticFunc.getUserId(this));
		LogUtil.v(TAG, "Params : " + params.toString());
		client.post(API.GET_BOOKING, params, new MyAsyncHttpResponseHandler(API.GET_BOOKING, this) {

			@Override
			protected void onAsyncSuccess(final Object res)
					throws JSONException {

				final JSONArray object = (JSONArray) res;

				list.clear();
				final JSONArray items = object.getJSONObject(0).getJSONArray("item");
				for (int i = 0; i < items.length(); i++) {
					list.add(items.getJSONObject(i));
				}

				adapter.setLoading(false);
				adapter.notifyDataSetChanged();
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
		final JSONObject object = list.get(position);

		final Intent intent = new Intent(this, PostDetailFragment.class);
		intent.putExtra("detail", object.toString());
		intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		startActivity(intent);
	}

	private class MyAdapter extends BaseAdapter implements View.OnClickListener {

		private boolean isLoading;

		public void setLoading(final boolean isLoading) {
			this.isLoading = isLoading;
		}

		@Override
		public int getCount() {
			if (this.isLoading) {
				return 1;
			}
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			if (this.isLoading) {
				return null;
			}
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (this.isLoading) {
				convertView = inflater.inflate(R.layout.loading_item, parent, false);
				convertView.setId(0x0123);
				return convertView;
			}

			if ((convertView != null) && (convertView.getId() == 0x0123)) {
				convertView = null;
			}

			final ItemHolder holder;
			if (null == convertView) {
				convertView = inflater.inflate(R.layout.feed_item_layout, parent, false);
				holder = new ItemHolder();

				holder.icon = (ImageView) convertView.findViewById(R.id.feed_profile_image);
				holder.name = (TextView) convertView.findViewById(R.id.feed_username);
				holder.price = (TextView) convertView.findViewById(R.id.feed_price);
				holder.dateTime = (TextView) convertView.findViewById(R.id.feed_time);
				holder.brand = (TextView) convertView.findViewById(R.id.post_brand);
				holder.model = (TextView) convertView.findViewById(R.id.feed_title);

				convertView.setTag(holder);
			} else {
				holder = (ItemHolder) convertView.getTag();
			}

			try {
				final JSONObject obj = list.get(position);

				final String icon = obj.getString("icon");
				if (icon == null || icon.length() == 0) {
					holder.icon.setImageBitmap(null);
				} else {
					imageLoader.displayImage(obj.getString("icon"), holder.icon);
				}

				holder.name.setText(obj.getString("user_name"));
				holder.brand.setText("Brand : " + obj.getString("brand_name"));
				holder.model.setText(obj.getString("title"));
				holder.dateTime.setText(obj.getString("time"));
				holder.price.setText("Price : " + obj.getString("price") + " Baht");

			} catch (final JSONException e) {
				LogUtil.e(TAG, e.getMessage(), e);
			}
			return convertView;
		}
		@Override
		public void onClick(View v) {

		}
	}

	private static class ItemHolder {
		ImageView icon;
		TextView name;
		TextView brand;
		TextView model;
		TextView dateTime;
		TextView price;
	}
}
